/*
 * @Author: wangyongcun
 * @Date: 2023-02-15 15:41:26
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2024-09-25 23:19:08
 * @Description: 导出所有的路由并注册 & 路由拦截
 */

// 从vue-router中获取创建路由实例方法
import { createRouter, createWebHistory } from "vue-router";
// 获取用户信息，用来判断是否登录过期
import { userStore } from "@/stores/userInfo";
import { dicStore } from "@/stores/dic";

export const routes = [
  // 登录路由
  {
    path: '/login',
    name: 'login',
    component: () => import('@/pages/login/index.vue'),
  },
  // layout 路由
  {
    path: '/',
    redirect: { name: 'course' },
    component: () => import('@/pages/layout/index.vue'),
    children: [
      // 首页
      {
        path: '/home',
        name: 'home',
        title: '首页',
        show: true,
        icon: "Coin",
        component: () => import('@/pages/home/index.vue'),
      },
      // 用户管理
      {
        path: '/user',
        name: 'user',
        title: '用户管理',
        show: true,
        icon: "Coin",
        component: () => import('@/pages/user/index.vue'),
      },
      {
        path: '/user/addOrEdit',
        name: 'userAddOrEdit',
        title: '新增用户',
        show: false,
        icon: "Coin",
        component: () => import('@/pages/user/addOrEdit.vue'),
      },
      // 场地管理
      {
        path: '/address',
        name: 'address',
        title: '场地管理',
        show: true,
        icon: "Coin",
        component: () => import('@/pages/address/index.vue'),
      },
      // 课程管理
      {
        path: '/course',
        name: 'course',
        title: '课程管理',
        show: true,
        icon: "Coin",
        component: () => import('@/pages/course/index.vue'),
      },
      {
        path: '/course/addOrEdit',
        name: 'courseAddOrEdit',
        title: '新增课程',
        show: false,
        icon: "Coin",
        component: () => import('@/pages/course/addOrEdit.vue'),
      },
      // 课表管理
      {
        path: '/timetable',
        name: 'timetable',
        title: '课表管理',
        show: true,
        icon: "Coin",
        component: () => import('@/pages/timetable/index.vue'),
      },
      {
        path: '/timetable/addOrEdit',
        name: 'timetableAddOrEdit',
        title: '新增课表',
        show: false,
        icon: "Coin",
        component: () => import('@/pages/timetable/addOrEdit.vue'),
      },
      // 课程类型管理
      {
        path: '/courseType',
        name: 'courseType',
        title: '课程类型管理',
        show: true,
        icon: "Coin",
        component: () => import('@/pages/courseType/index.vue'),
      },
      // 新增课程类型
      {
        path: '/courseType/addOrEdit',
        name: 'courseTypeAddOrEdit',
        title: '新增课程类型',
        show: false,
        icon: "Coin",
        component: () => import('@/pages/courseType/addOrEdit.vue'),
      },
      // 授课形式管理
      {
        path: '/teachingFormatType',
        name: 'teachingFormatType',
        title: '授课形式管理',
        show: true,
        icon: "Coin",
        component: () => import('@/pages/teachingFormatType/index.vue'),
      },
      // 新增授课形式
      {
        path: '/teachingFormatType/addOrEdit',
        name: 'teachingFormatTypeAddOrEdit',
        title: '新增授课形式',
        show: false,
        icon: "Coin",
        component: () => import('@/pages/teachingFormatType/addOrEdit.vue'),
      },
      // 用户类型管理
      {
        path: '/userType',
        name: 'userType',
        title: '用户类型管理',
        show: true,
        icon: "Coin",
        component: () => import('@/pages/userType/index.vue'),
      },
      {
        path: '/userType/addOrEdit',
        name: 'userTypeAddOrEdit',
        title: '新增用户类型',
        show: false,
        icon: "Coin",
        component: () => import('@/pages/userType/addOrEdit.vue'),
      },
      // 场地类型管理
      {
        path: '/addressType',
        name: 'addressType',
        title: '场地类型管理',
        show: true,
        icon: "Coin",
        component: () => import('@/pages/addressType/index.vue'),
      },
      {
        path: '/addressType/addOrEdit',
        name: 'addressTypeAddOrEdit',
        title: '新增场地类型',
        show: false,
        icon: "Coin",
        component: () => import('@/pages/addressType/addOrEdit.vue'),
      },
      {
        path: '/address/addOrEdit',
        name: 'addressAddOrEdit',
        title: '新增场地',
        show: false,
        icon: "Coin",
        component: () => import('@/pages/address/addOrEdit.vue'),
      },
      // 用户管理
      {
        path: '/order',
        name: 'order',
        title: '订单管理',
        show: true,
        icon: "Coin",
        component: () => import('@/pages/order/index.vue'),
      },


      // 权限管理
      {
        path: '/permission',
        name: 'permission',
        title: '权限包管理',
        show: true,
        icon: "Coin",
        component: () => import('@/pages/permission/index.vue'),
      },
      // 新增权限包
      // {
      //   path: '/permission/addOrEdit',
      //   name: 'permissionAddOrEdit',
      //   title: '新增权限包',
      //   show: false,
      //   icon: "Coin",
      //   component: () => import('@/pages/permission/addOrEdit.vue'),
      // },
      // 新增权限点
      // {
      //   path: '/permission/addOrEditPoint',
      //   name: 'permissionAddOrEditPoint',
      //   title: '新增权限点',
      //   show: false,
      //   icon: "Coin",
      //   component: () => import('@/pages/permission/addOrEditPoint.vue'),
      // },
    ]
  },
];

const router = createRouter({
  history: createWebHistory('/manageSystem/'),
  routes: routes
});
// 路由守卫
router.beforeEach((to, from, next) => {
  const userInfo = userStore();
  const sessionUserInfo = sessionStorage.getItem("userInfo");
  // 持久化信息获取
  if (!userInfo.$state.id && sessionUserInfo) {
    userInfo.$state = JSON.parse(sessionUserInfo);
  }
  // 如果token存在，则放行
  if (userInfo.$state.token) {
    // 处理菜单
    // @ts-ignore
    userInfo.menuList = routes[1].children.filter(x => x.show);
    // 放行
    next();
  }
  // 跳转至登录页
  else {
    to.path == "/login" ? next() : next("/login");
  }
});

// pinia持久化（监听页面卸载并将pinia的信息加密存入）
window.addEventListener("unload", () => {
  const userInfo = userStore();
  const dicInfo = dicStore();
  sessionStorage.setItem("userInfo", JSON.stringify(userInfo.$state));
  sessionStorage.setItem("dicInfo", JSON.stringify(dicInfo.$state));
});
// 删除敏感信息
window.addEventListener("load", () => {
  sessionStorage.removeItem("userInfo");
  const sessionDicInfo = sessionStorage.getItem("dicInfo");
  const dicInfo = dicStore();
  if (!dicInfo.$state?.length && sessionDicInfo) {
    dicInfo.$state = JSON.parse(sessionDicInfo);
  }
});

export default router;
